package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.html.HTMLFilter;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.apache.ibatis.annotations.Param;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.sql.rowset.BaseRowSet;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult add(@PathVariable String name,@PathVariable Integer age,@PathVariable String sex){
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
     /*   review.setCreateBy(SecurityUtils.getUsername());
        review.setCreateTime(DateUtils.getNowDate());
        review.setUpdateBy(SecurityUtils.getUsername());
        review.setUpdateTime(DateUtils.getNowDate());*/
        return toAjax(reviewService.add(review));
    }

    @PostMapping("/saveData")
    public AjaxResult save(@RequestBody Review review){
      /*  review.setCreateBy(SecurityUtils.getUsername());
        review.setCreateTime(DateUtils.getNowDate());
        review.setUpdateBy(SecurityUtils.getUsername());
        review.setUpdateTime(DateUtils.getNowDate());*/
        return toAjax(reviewService.add(review));
    }
    /**=========================================================删除数据=============================================*/

    @DeleteMapping("/remove/{id}")
    public AjaxResult deleteId(@PathVariable Long id ){
        return toAjax(reviewService.deleteById(id));
    }
    /**=========================================================修改数据=============================================*/

    @PostMapping("/update")
    public AjaxResult update(@RequestBody Review review){
     /*   review.setUpdateBy(SecurityUtils.getUsername());
        review.setUpdateTime(DateUtils.getNowDate());*/
        return toAjax(reviewService.update(review));
    }

    /**=========================================================查询数据=============================================*/
    @GetMapping("/getById")
    public AjaxResult selectById(Long id){
        Review review = reviewService.selectById(id);
        return AjaxResult.success(review);
    }

    @GetMapping("/getDataByPage")
    public TableDataInfo selectPage(Review review){
        /*Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        List<Review> list = reviewService.selectByPage(pageNum, pageSize);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(list);
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("操作成功");
        Integer count = reviewService.selectCount();
        tableDataInfo.setTotal(count);*/
        List<Review> list = reviewService.selectByPage(review);
        return getDataTablePage(list);
    }


}